package com.softme.trade.engine.setup;

import java.util.List;

import com.softme.trade.model.Language;
import com.softme.trade.model.Role;
import com.softme.trade.model.User;
import com.softme.trade.model.dao.RoleDAO;
import com.softme.trade.model.dao.UserDAO;
import com.softme.trade.util.BeanFactory;

/**
 * Classe criada para fazer setup dos usuários padrões ao sistema
 * 
 * @author Mews
 * 
 */
public class SetupUser implements Setup
{
	UserDAO userDAO = (UserDAO) BeanFactory.getBean("userDAO", UserDAO.class);

	RoleDAO roleDAO = (RoleDAO) BeanFactory.getBean("roleDAO", RoleDAO.class);

	public boolean canRun()
	{
		List<User> list = this.userDAO.loadAll();

		if (list.size() >= 2)
			return false;

		return true;
	}

	public boolean run()
	{
		//Adm
		addUser(true, "adm@adm", "adm", "123", "Administrador", new Long(7), Language.PT_BR);
		//Guest
		addUser(true, "guest@guest", "guest", "123", "Convidado", new Long(2), Language.PT_BR);
		//Comercial
		addUser(true, "comercial@guest", "comercial", "123", "Comercial", new Long(3), Language.PT_BR);

		return true;
	}

	private void addUser(boolean active, String email, String login, String password, String name,
			long roleId, Language language)
	{
		User user = new User();

		user.setActive(active);
		user.setEmail(email);
		user.setLogin(login);
		user.setPassword(password);
		user.setName(name);
		user.setLanguage(language);

		Role role = this.roleDAO.load(roleId);

		if (role != null)
			user.setRole(role);

		this.userDAO.save(user);
	}
}
